package com.zHan.sky.service.impl;

import com.zHan.sky.common.contant.StatusConstant;
import com.zHan.sky.entity.Orders;
import com.zHan.sky.mapper.OrdersMapper;
import com.zHan.sky.service.*;
import com.zHan.sky.vo.BusinessDataVO;
import com.zHan.sky.vo.DishOverViewVO;
import com.zHan.sky.vo.OrderOverViewVO;
import com.zHan.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * ClassName: WorkSpaceServiceImpl
 * Description:
 * date: 2023/8/1 0001 21:42
 *
 * @author zHan
 */
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        // 查询营业额
        Double turnover = ordersMapper.selectTotalAmount(begin, end, Orders.COMPLETED);
        // 如果营业额为空
        turnover = turnover == null? 0.0 : turnover;
        // 查询有效订单数
        Integer validOrderCount = ordersService.getOrderCount(begin, end, Orders.COMPLETED);
        // 总订单数
        Integer totalOrderCount = ordersService.getOrderCount(begin, end, null);
        // 订单完成率
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }
        // 平均客单价
        Double unitPrice = 0.0;
        if (validOrderCount != 0) {
            unitPrice = turnover / validOrderCount;
            // 保留两位小数
            unitPrice =
                    new BigDecimal(unitPrice).setScale(2, RoundingMode.HALF_UP).doubleValue();
        }

        // 查询新增用户数
        Integer newUsers = userService.getUserCount(begin, end);
        // 返回数据
        return BusinessDataVO
                .builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders(LocalDateTime begin, LocalDateTime end) {
        // 待接单
        Integer waitingOrders = ordersService.getStatusCount(begin, end,
                Orders.TO_BE_CONFIRMED);
        // 待派送
        Integer deliveryInProgress = ordersService.getStatusCount(begin, end, Orders.CONFIRMED);
        // 已完成
        Integer completedOrders =
                ordersService.getStatusCount(begin, end, Orders.COMPLETED);
        // 已取消
        Integer cancelledOrders =
                ordersService.getStatusCount(begin, end, Orders.CANCELLED);
        // 全部订单
        Integer allOrders =
                ordersService.getStatusCount(begin, end, null);

        return OrderOverViewVO
                .builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveryInProgress)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品预览
     *
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        // 已启售数量
        Integer sold = dishService.getStatusCount(StatusConstant.ENABLE);
        // 已停售数量
        Integer discontinued = dishService.getStatusCount(StatusConstant.DISABLE);

        return DishOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐预览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        // 已启售数量
        Integer sold = setmealService.getStatusCount(StatusConstant.ENABLE);
        // 已停售数量
        Integer discontinued = setmealService.getStatusCount(StatusConstant.DISABLE);

        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
